package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.LoginCustomerService;
import com.chixing.util.AESUtil;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/17/3:33
 * @Description:
 */
@Service
public class LoginCustomerServiceImpl implements LoginCustomerService {

    private static KeyPair keyPair;
    private static int codepassword;
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String consumerEmail;

    public void setEmail(String consumerEmail) {
        this.consumerEmail = consumerEmail;
    }








    @Override
    public ServerResponse Login(Long custTelno, String custPassword) {
        System.out.println("登录密码:"+custPassword);

        String encrypted = custPassword;
        String privateKey = getPrivateKey1();
        System.out.println(privateKey);

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            custPassword = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("consumerPassword:"+custPassword);
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelnoEqualTo(custTelno);
        List<Customer> consumerList = customerMapper.selectByExample(example);
        if (consumerList.size()>0){
            Customer customer = consumerList.get(0);
            String getPassword = customer.getCustPassword();
            System.out.println("数据库读取到的密码:"+getPassword);
            if(custPassword.equals(getPassword)){
                return ServerResponse.getSucccess(customer);
            }
        }

        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getPublicKey1() {
        try {
            keyPair = RSAUtils.genKeyPair(1024);
            PublicKey publicKey=keyPair.getPublic();
            System.out.println("公钥：");
            System.out.println("公钥输额信息");
            String pubKey= new String(Base64.getEncoder().encode(publicKey.getEncoded()));
            System.out.println("ghjjhj"+pubKey);
            return ServerResponse.getSucccess(pubKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public String getPrivateKey1() {
        try {
            PrivateKey privateKey=keyPair.getPrivate();
            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));
            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    //邮箱


    @Override
    public ServerResponse getByEmail(String custEmail) {
        System.out.println("前段返回的邮箱"+custEmail);
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustEmailEqualTo(custEmail);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if(customerList.size()>0){
            Customer customer = customerList.get(0);
            String getCustEmail = customer.getCustEmail();
            System.out.println("数据库读取到的邮箱:" + getCustEmail);
            if(custEmail.equals(getCustEmail)){
                SimpleMailMessage msg = new SimpleMailMessage();
                msg.setFrom("wtl113114@163.com");
                //标题
                msg.setSubject("思必行租车");
                //验证码是随机的10000~99999
                codepassword =  (int) (Math.random()*90000+10000);
                msg.setText("验证码:"+codepassword);
                msg.setTo(custEmail);//登录页面输入的 - 收件方
                //真正的发送
                mailSender.send(msg);
                return ServerResponse.getSucccess(customer);
            }
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByEmailAndPassword(String custEmail, Integer passwordEmail) {
        System.out.println("前段返回的邮箱："+custEmail);
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustEmailEqualTo(custEmail);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if(customerList.size()>0){
            Customer customer = customerList.get(0);
            String getCustEmail  = customer.getCustEmail();
            System.out.println("数据库获得的邮箱"+ getCustEmail);
            if(passwordEmail.equals(codepassword)){
                return  ServerResponse.getSucccess("Validation-succeeded");
            }
        }
        return ServerResponse.getFail("Validation-fail");
    }
}
